/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.tunnel.db.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

@Mapper
public interface UserMapper extends BaseMapper<UserDO> {
    public default UserDO findByUserId(Integer userId) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("deleted", OkrConstants.NOT_DELETED);
        return selectOne(queryWrapper);
    }

    @Select("SELECT u.* FROM t_user u,t_department_user du WHERE u.id=du.user_id AND du.department_id=#{deptId}")
    public List<UserDO> findByDeptId(Integer deptId);

    public default List<UserDO> findListByDeptIdList(List<Integer> deptIdList) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id", deptIdList);
        queryWrapper.eq("deleted", OkrConstants.NOT_DELETED);
        return selectList(queryWrapper);
    }

    public default UserDO findByDingUserId(String duserId) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ding_user_id", duserId);
        queryWrapper.eq("deleted", OkrConstants.NOT_DELETED);
        return selectOne(queryWrapper);
    }

    @Select("SELECT * FROM t_user u where u.deleted=0 and u.`status`=1 and u.account=#{account}")
    UserDO findUserByAccount(@Param("account") String account);

    @Select("SELECT * FROM t_user u where u.deleted=0 and u.status=1 and u.account=#{account} and u.password=#{password}")
    UserDO findUserByAccountAndPassword(@Param("account") String account, @Param("password") String password);


    @Select("SELECT * FROM t_user u where u.deleted=0 and u.status=1 and u.user_id=#{userId}")
    UserDO findUserByUserId(@Param("userId") Integer userId);

    @Select("SELECT * FROM t_user u where u.deleted=0 and u.status=1 and u.user_id=#{userId} and u.password=#{password}")
    UserDO findUserByUserIdAndPassword(@Param("userId") Integer userId, @Param("password") String password);

    List<UserDO> selectByIdList(@Param("userIdList") Set<Integer> userIdList);

    @Select("select *from t_user where identity_source_id = #{identitySourceId}")
    List<UserDO> queryByIdentitySourceId(Integer identitySourceId);

    @Select("SELECT u.* FROM t_user u,t_department_user du WHERE du.user_id!=#{userId} AND du.department_id=#{depamentId} AND u.id=du.user_id;")
    List<UserDO> queryNoOneDepartment(Integer userId,Integer depamentId);

}
